/* Janus Chat - Version 1.0
 * Authors: Matthew Perry
 *          Michael Freid
 *          Marc Latou
 *          Richard Lee
 *
 * The Janus Chat Client is a highly secured
 * peer-to-peer chat client designed to facilitate
 * the transfer of sensitive data or as a communication
 * medium for the extra paranoid
 *
 * This file contains the unit tests for the socket class */


#include <string>
#include <iostream>
#include "../lib/message.h"
#include "../lib/socket.h"
#include "../lib/exceptions.h"

int main(){

    int myInt;
    struct sockaddr_in something;

    try
    {
        //Something that throws an exception
        Socket s1;  //test the default constructor
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        myInt = 49152;
        /*something.peerInfo = ;
        something.peerAddr = ;
        something.peerPort = ;*/
        Socket s2( myInt, something ); //test the second constructor
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    Socket s2( myInt, something ); //make this for general use, assuming that the constructor test above is successful

    try
    {
        //Something that throws an exception
        //something.peerInfo  //try modifying the struct
        Socket s3( myInt, something );
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        //something.peerAddr  //manipulate the struct
        Socket s4( myInt, something );
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        //s1.peerPort  //manipulate the struct
        Socket s5( myInt, something );
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        myInt = 49165;
        /*something.peerInfo= ;  //after testing the struct, set it back into "normal" parameters
        something.peerAddr= ;
        something.peerPort= ;*/
        Socket s6( myInt, something, struct timeval 4 );  //test the final constructor
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    Socket s6( myInt, something, struct timeval 4 ); //make this for general use, assuming the constructor test above is successful




    /*std::cout<<"Please enter an integer: "<<std::endl;
    std::cin>>myInt;


    std::cout<<"Please enter any dest. ip:" <<std::endl;
    std::cin>>destip;
    std::cout<<"Please enter any dest. port:" <<std::endl;
    std::cin>>destport;*/

    try
    {
        //Something that throws an exception
        s2.connectTo( "127.0.0.1", 49165); //test the connection with a valid ip address and open socket
        std::cout<<"You should now be connected to "<<s2.getAddr()<<" on port "<<s2.getPort()<<std::endl;

    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
        Message q(1, "MessageChecker", "This is a test message.");
        s2.sendMsg( q );  //test sendMsg
        std::cout<<"You have recieved the following message:"<<std::endl;
        std::cout<<( s6.recvMsg( ) ).getBody()<<std::endl;  //test recvMsg
        }
    }


    try
    {
        //Something that throws an exception
        Message q2( 3, "Angelina", "This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just because...This is the song that never ends, Yes, it goes on and on, my friends. Some people started singing it, not knowing what it was, and they'll continue singing it forever just b" );
        s2.sendMsg(q2);  //make sure we get the exception on a message that is too long
    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        s2.connectTo( "badipaddress" , 49166); //give a bad ip address
        std::cout<<"You should now be connected to "<<s2.getAddr()<<" on port "<<s2.getPort()<<std::endl;

    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    try
    {
        //Something that throws an exception
        s2.connectTo( "127.0.0.1" , 764849); //give a bad port number
        std::cout<<"You should now be connected to "<<s2.getAddr()<<" on port "<<s2.getPort()<<std::endl;

    }
    catch( BaseException* e )
    {
        //code that handles exception
        std::cout << "Error: " << e->what() << std::endl;
    }

    //close all sockets opened
    s2.closeSock();
    s6.closeSock();

    std::cout<<"All tests completed.";

    return 0;
}
